from models.models import Machine
from service.BaseService import BaseService
from sqlmodel import Session, select
from core.Utils import generate_uuid, en_password
from typing import Optional

class MachineService(BaseService[Machine]):
    def __init__(self):
        super().__init__(Machine)

    def get_machine_by_id(self,machine_id: str):
        statement = select(Machine).where(Machine.machine_id == machine_id)
        return self.session.exec(statement).first()



    def create_machine(self,machine_point: dict,area_id: str, user_id: str):
        machine_id = generate_uuid()
        new_machine = Machine(machine_id=machine_id, machine_point=machine_point, machine_battery=100,
                                   area_id=area_id, status=1,create_by=user_id,
                                   update_by=user_id)

        return self.create(new_machine)


    def get_machine_list(self,page: int, page_size: int, status: Optional[int] = None,
                         machine_id: Optional[str] = None, area_id : Optional[str] = None):
        filters = []
        # 添加过滤条件
        if status is not None:
            filters.append(Machine.status == status)
        if machine_id is not None:
            filters.append(Machine.machine_id == machine_id)
        if area_id is not None:
            filters.append(Machine.area_id == area_id)
        # 获取分页数据
        machine_list, total = self.get_paginated(page, page_size, filters)
        return machine_list, total



